$.fn.serializeObject = function()    
{    
   var o = {};    
   var a = this.serializeArray();    
   $.each(a, function() {    
       if (o[this.name]) {    
           if (!o[this.name].push) {    
               o[this.name] = [o[this.name]];    
           }    
           o[this.name].push(this.value || '');    
       } else {    
           o[this.name] = this.value || '';    
       }    
   });    
   return o;    
};  

var params = {};
var current = {};
var defaultPageSize = 10;
var currentPage = 1;
var pageSize = defaultPageSize;
var option = {};

//物料类别需要用到的变量
var matParams = {};
var matCurrent = {};
var matDefaultPageSize = 10;
var matCurrentPage = 1;
var matPageSize = matDefaultPageSize;
var matOption = {};

$("ul.tabtit").on("click",function(event){
	$('.main_panel').load(webUrl+"public/page/customer/customerQuery.html");
});

$(function(){
	$("#salesmanName").bind('click',function(){employee()});
	var id=localStorage.getItem("rowId");
	localStorage.removeItem("rowId");
	getMaterielById(id);
	
	$('.add').on('click',saveScene);
	$('.empty').on('click',back);
	$('.check').on('click',queryLov);
	$('.copy').on('click',queryMaterielLov);
	$("#vendorName").click(function(){
		option.url = webUrl+'chwVendor/findchwVendor.do';
		option.type="user";				//div名称前缀user_dialogbox
		option.table="customerGrid";	//div中table表名
		option.obj="wuliao";				//回调方法中传值做标识
		option.columns = [
		                  {field:'vendorId',checkbox:true},
				          {field:'_itemNo',title:'序号',align:'center',width:50,formatter:formatId},
				          {field:'vendorName',title:'供应商名称',align:'center',width:150,sortable:true},
				          {field:'vendorContactName',title:'供应商联系人',align:'center',width:150,sortable:true},
				          {field:'remark',title:'备注',align:'center',width:150,sortable:true},
				      ];
		option.onSelect=function(rowIndex, rowData){ $("#vendorName").val(rowData.vendorName); $("#vendorId").val(rowData.vendorId);};
		Lov(option);
	});
	
	$("#materielTypeName").click(function(){
		matOption.url = webUrl+'sysParams/getSysParamsList.do';
		matOption.type="materielType";				//div名称前缀user_dialogbox
		matOption.table="materielTypeGrid";	//div中table表名
		matOption.obj="materiel";				//回调方法中传值做标识
		matParams.typeCode="MaterielCode";
		matOption.columns = [
		                  {field:'id',checkbox:true},
				          {field:'_itemNo',title:'序号',align:'center',width:50,formatter:formatId},
				          {field:'param_value',title:'物料类别名称',align:'center',width:150,sortable:true},
				          {field:'note',title:'备注',align:'center',width:150,sortable:true},
				      ];
		matOption.onSelect=function(rowIndex, rowData){ $("#materielTypeName").val(rowData.param_value); $("#materielTypeId").val(rowData.id);};
		matLov(matOption);
	});
});

function getMaterielById(id){
	$.ajax({
		url:webUrl+'customer/findChwCustomerById.notPermSc',
		type:'post',
		data:{id:id}
	}).done(function(data){
		var d = data.bo;
		//alert(JSON.stringify(d));
		$("#customerName").val(d.customerName);
		$("#customerId").val(d.customerId);
		if(d.customerSex=="男"){
			$("#radio1").attr("checked", "checked"); 
		}
		if(d.customerSex=="女"){
			$("#radio2").attr("checked", "checked"); 
		}
		$("#customerPhone").val(d.customerPhone);
		$("#customerAge").val(d.customerAge);
		$("#bankAccountName").val(d.bankAccountName);
		$("#bankAccountNum").val(d.bankAccountNum);
		$("#houseStyle").val(d.houseStyle);
		$("#area").val(d.area);
		$("#property").val(d.property);
		$("#customerAddress1").val(d.customerAddress1);
		$("#customerAddress2").val(d.customerAddress2);
		$("#customerAddress3").val(d.customerAddress3);
		$("#remark").val(d.remark);
		$("#customerType").val(d.customerType);
		if(d.salesmanId!=null){
			$("#salesmanId").val(d.salesmanId);
		}
		if(d.salesmanName!=null){
			$("#salesmanName").val(d.salesmanName);
		}
	});
}

function validateNum(objName){  
    var reg = new RegExp("^[0-9]*$");  
    var obj = document.getElementById(objName);  
	 if(!reg.test(obj.value)){  
	     alert("请输入数字!");  
	 }  
	/* if(!/^[0-9]*$/.test(obj.value)){  
	     alert("请输入数字!");  
	 }  */
}  

function queryLov(){
	if($("#vendorNameLov").val()!=""){
		params.vendorName=$("#vendorNameLov").val();
	}
	if($("#vendorContactNameLov").val()!=""){
		params.vendorContactName=$("#vendorContactNameLov").val();
	}
	option.url = webUrl+'chwVendor/findchwVendor.do';
	option.type="user";				//div名称前缀user_dialogbox
	option.table="customerGrid";	//div中table表名
	option.obj="wuliao";				//回调方法中传值做标识
	option.columns = [
	                  {field:'vendorId',checkbox:true},
			          {field:'_itemNo',title:'序号',align:'center',width:50,formatter:formatId},
			          {field:'vendorName',title:'供应商名称',align:'center',width:150,sortable:true},
			          {field:'vendorContactName',title:'供应商联系人',align:'center',width:150,sortable:true},
			          {field:'remark',title:'备注',align:'center',width:150,sortable:true},
			      ];
	option.onSelect=function(rowIndex, rowData){ $("#vendorName").val(rowData.vendorName); $("#vendorId").val(rowData.vendorId);};
	Lov(option);
}

function queryMaterielLov(){
	
}

function goback(obj){
	if(obj=="wuliao"){
		var vendorId= $('input[name="vendorId"]:checked').val();
		$("#vendorId").val(vendorId);
	}
}

function back(){
	$('.main_panel').load(webUrl+"public/page/customer/customerQuery.html");
}

function saveScene(){
	var json={};
    json = $("#addDataFormHeader").serializeObject();
    json=JSON.stringify(json);
	if(checknull("customerName") ){
		$.ajax({
			url: webUrl+'customer/updateChwCustomer.do',
			type:'post',
			data:{param:json}
		}).done(function(data){
			var result = data.bo;
			if(result==0){
				$.messager.confirm('提示 ', '修改成功！', function(r){
					$('.main_panel').load(webUrl+"public/page/customer/customerQuery.html");
				});
			}else{
				$.messager.alert('提示 ', '修改失败！', 'info');
			}
		});
	}
}



//人员LOV用到的变量 (采购员)
var employeeParams = {};
var employeeCurrent = {};
var employeeDefaultPageSize = 10;
var employeeCurrentPage = 1;
var employeePageSize = employeeDefaultPageSize;
var employeeOption = {};
function employee(){
	employeeOption.url = webUrl+'chwEmployee/findEntity.do';
	employeeOption.type="buyer";				//div名称前缀user_dialogbox
	employeeOption.table="buyerGrid";	//div中table表名
	employeeParams.typeCode="FK";
	var paramValue=$("#employeeNameQuery").val();
	if(paramValue!=""){
		employeeParams.employeeName=paramValue;
	}
	employeeOption.columns = [ {field : 'employeeId',checkbox : true	}, 
	{field : '_itemNo',	title : '序号',	align : 'center',	width : 50,	formatter : formatId}, 
	{field : 'employeeName',title : '姓名',	align : 'center',	width : 150,sortable : true	}, 
	{field : 'employeeSex',	title : '性别',	align : 'center',	width : 150,sortable : true,}, ];
	employeeOption.onSelect=function(rowIndex, rowData){ $("#salesmanName").val(rowData.employeeName);$("#salesmanId").val(rowData.employeeId); };
	employeeLov(employeeOption);
}